/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package route;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import route.bean.FilterBean;

/**
 *
 * @author meluo
 * @Data 2016/2/13
 */
public class Utils {

    /**
     * 实例化所有的Filter
     *
     * @param listFB 需要实例化参数的地址
     * @param map 网络传递的参数
     * @return 已经实例过的Filter的集合
     * @throws Exception 异常抛出
     */
    public static List<AbsFilter> refFilter(List<FilterBean> listFB, Map<String, String[]> map, HttpServletRequest request, ServletResponse response) throws Exception {
        List<AbsFilter> listAF = new ArrayList<>();
        for (FilterBean fb : listFB) {
            Class<?> filter = Class.forName(fb.getRefClass());
            AbsFilter af = (AbsFilter) filter.newInstance();
            refField(filter, map, af);
            af.setRequest(request);
            af.setResponse(response);
            af.setErrInfo(fb.getErrinfo());
            af.setRetpage(fb.getRetPage());
            listAF.add(af);
        }
        return listAF;
    }

    public static void refField(Class<?> bean, Map<String, String[]> map, Object ob) throws Exception {
        Field[] f = bean.getDeclaredFields();
        for (int i = 0; i < f.length; i++) {
            Field fi = f[i];
            fi.setAccessible(true);
            String si[] = map.get(fi.getName());
            if(si!=null&&si.length!=0){
               String s=si[0];
            if (s != null && !"".equals(s)) {
                String type = fi.getType().toString();

                if (type.endsWith("String")) {
                    fi.set(ob, s);
                } else if (type.endsWith("int") || type.endsWith("Integer")) {
                    fi.set(ob, Integer.parseInt(s));
                } else if (type.endsWith("boolean") || type.endsWith("Boolean")) {
                    fi.set(ob, Boolean.parseBoolean(s));
                } else if (type.endsWith("float") || type.endsWith("Float")) {
                    fi.set(ob, Float.parseFloat(s));
                } else if (type.endsWith("double") || type.endsWith("Double")) {
                    fi.set(bean, Double.parseDouble(s));
                } else if (type.endsWith("char") || type.endsWith("Character")) {
                    fi.set(ob, map.get(fi.getName())[0].charAt(0));
                } else if (type.endsWith("long") || type.endsWith("Long")) {
                    fi.set(ob, Long.parseLong(s));
                } else if (type.endsWith("byte") || type.endsWith("Byte")) {
                    fi.set(ob, Byte.parseByte(s));
                } else if (type.endsWith("short") || type.endsWith("Short")) {
                    fi.set(ob, Short.parseShort(s));
                } else {
                    System.out.println(fi.getName() + "不是基本数据类型");
                }
            }
            }
        }
    }
}
